package com.net.oil.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.net.oil.action.ActionDetailActivity;
import com.net.oil.app.Constant;
import com.net.oil.coupon.CouponActivity;
import com.net.oil.order.ReserveDetailActivity;
import com.net.oil.push.bean.PushBean;
import com.net.oil.util.StringUtil;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JIGUANG-Example";
    private Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;
        try {
            Bundle bundle = intent.getExtras();
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                //SDK 向 JPush Server 注册所得到的注册 ID 。
                //一般来说，可不处理此广播信息。
            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                //收到了自定义消息 Push 。

            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                //收到了通知 Push
            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                //用户点击了通知
                if (bundle != null && !StringUtil.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))){
                    notificationHandle(bundle.getString(JPushInterface.EXTRA_EXTRA));
                }

            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
                //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
                //JPush 服务的连接状态发生变化（注：不是指 Android 系统的网络连接状态。）
            } else {
                //
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private void notificationHandle(String json){
        Gson gson = new Gson();
        PushBean pushBean = gson.fromJson(json, PushBean.class);

        switch (pushBean.getNotificationType()){
            case "1":
                break;
            case "2":
                Intent intent3 = new Intent(context, ActionDetailActivity.class);
                intent3.putExtra(Constant.INTENT_ACTION_KEY, Integer.parseInt(pushBean.getNotificationBusiId()));
                context.startActivity(intent3);
                break;
            case "3":
                Intent intent1 = new Intent(context, PushHandleActivity.class);
                intent1.putExtra(Constant.INTENT_PUSH_JSON, json);
                context.startActivity(intent1);
                break;
            case "4":
                Intent intent2 = new Intent(context, PushHandleActivity.class);
                intent2.putExtra(Constant.INTENT_PUSH_JSON, json);
                context.startActivity(intent2);
                break;
            case "5":
                break;
            case "6":
//                context.startActivity(new Intent(context, ReserveActivity.class));
                Intent intent = new Intent(context, ReserveDetailActivity.class);
                intent.putExtra(Constant.INTENT_RESERVE_ID_KEY, Integer.parseInt(pushBean.getNotificationBusiId()));
                context.startActivity(intent);
                break;
            case "7":
                context.startActivity(new Intent(context, CouponActivity.class));
                break;
            default:
                break;
        }
    }

}
